#!/bin/bash
# 脚本测试，主机环境：Ubuntu-20.04
#监控CPU，磁盘，内存
#现在的时间过8小时后，包括小时分钟时间
now=$(date -d '8 hour' '+%Y-%m-%d %H-%M-%S');
#cpu使用阈值
cpu_warn='75';
#mem内存使用阈值
mem_warn='100';
#磁盘使用阈值
disk_warn='30';
#---cpu
item_cpu(){
        #cpu空余
        cpu_idle=$(top -b -n 1 | grep Cpu | awk '{print $8}' | cut -d "." -f 1)
        #占用cpu
        cpu_use=$(( 100 - ${cpu_idle} ))
        echo $now "当前CPU使用率为："${cpu_idle} >> /opt/cpu.log
        if [ $cpu_use -gt $cpu_warn ]
        then
                echo "cpu warning!!!"
        else
                echo "cpu ok."
        fi
}
#---mem
item_mem(){
        #单位为MB(找到free空余的和缓存区的内存)
        mem_free=$(free -m | grep Mem | awk '{print $4+$6}')
        echo $now "当前内存剩余空间为："${mem_free}MB >> /opt/mem.log
        if [ $mem_free -lt $mem_warn ]
        then
                echo "mem warning!!!"
        else
                echo "mem ok."
        fi
}
#--disk
item_disk(){
        disk_use=$(df -h | awk '$NF=="/"{printf "%d", $5}')
        echo $now "当前disk使用率为："${disk_use} >> /opt/disk.log
        if [ $disk_use -gt $disk_warn ]
        then
                echo "disk warning!!!"
        else
                echo "disk ok."
        fi
}
item_cpu
item_mem
item_disk

# root@ubuntu:~# df -h
# Filesystem      Size  Used Avail Use% Mounted on
# udev            1.9G     0  1.9G   0% /dev
# tmpfs           388M  2.0M  386M   1% /run
# /dev/sda5        20G  8.6G  9.5G  48% /
# tmpfs           1.9G     0  1.9G   0% /dev/shm
# tmpfs           5.0M  4.0K  5.0M   1% /run/lock
# tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
# /dev/loop0       56M   56M     0 100% /snap/core18/2128
# /dev/loop1      219M  219M     0 100% /snap/gnome-3-34-1804/72
# /dev/loop2      497M  497M     0 100% /snap/gnome-42-2204/141
# /dev/loop3       66M   66M     0 100% /snap/gtk-common-themes/1515
# /dev/loop4      128K  128K     0 100% /snap/bare/5
# /dev/loop5       75M   75M     0 100% /snap/core22/1033
# /dev/loop6       56M   56M     0 100% /snap/core18/2812
# /dev/loop7       51M   51M     0 100% /snap/snap-store/547
# /dev/loop9       13M   13M     0 100% /snap/snap-store/959
# /dev/loop10      92M   92M     0 100% /snap/gtk-common-themes/1535
# /dev/loop11     219M  219M     0 100% /snap/gnome-3-34-1804/93
# /dev/sda1       511M  4.0K  511M   1% /boot/efi
# tmpfs           388M   72K  388M   1% /run/user/1000
# /dev/loop12      41M   41M     0 100% /snap/snapd/20671
# /dev/loop8       75M   75M     0 100% /snap/core22/1122
# tmpfs           388M     0  388M   0% /run/user/0
# root@ubuntu:~# df -h | grep /dev/sda5 | awk '{print $2}'
# 20G


# root@ubuntu:~# free -m
#               total        used        free      shared  buff/cache   available
# Mem:           3876        1782         171          18        1922        1808
# Swap:           923           1         921
# root@ubuntu:~# free -m | grep Mem | awk '{print $6}'
# 1922

# root@ubuntu:~# top -b -n 1 | grep Cpu
# %Cpu(s):  2.9 us,  5.7 sy,  0.0 ni, 91.4 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
# root@ubuntu:~# top -b -n 1 | grep Cpu | awk '{print $8}'
# 94.1
# root@ubuntu:~# top -b -n 1 | grep Cpu | awk '{print $8}' | cut -d "." -f 1
# 91

# root@ubuntu:~# top -b -n 1 | grep Mem
# MiB Mem :   3876.4 total,    169.8 free,   1782.5 used,   1924.1 buff/cache
# MiB Swap:    923.3 total,    922.0 free,      1.3 used.   1808.0 avail Mem 
# root@ubuntu:~# top -b -n 1 | grep Mem | awk '{print $8}' | cut -d "u" -f 1 
# 1782.5
